package org.yxjme.yxj_ijkplayerlib;

import android.content.pm.ActivityInfo;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import org.yxjme.ijkplayerlibrary.android.YXJControlVideoView;
import org.yxjme.ijkplayerlibrary.android.YXJVideoView;
import org.yxjme.ijkplayerlibrary.ijk.i.IJKVideoListener;
import org.yxjme.ijkplayerlibrary.ijk.v.IJKVideoView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    /**
     */
    private IJKVideoView mIJKVideoView ;
    private YXJControlVideoView controlVideoView ;

    String videoUrl="http://playertest.longtailvideo.com/adaptive/wowzaid3/playlist.m3u8";
    String videoUrl1="https://taojinapp.oss-cn-hangzhou.aliyuncs.com/ProductImage/2111/22/2111221426410151.mp4";
    /*暂未图*/
    String videoUrl2="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F092221102018%2F210922102018-8-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641984227&t=1cf9485dffaf2d487754b04a2c23f893";

    YXJVideoView mYXJVideoVIew ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        // 隐藏标题栏
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        // 隐藏状态栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        mIJKVideoView = findViewById(R.id.mIJKVideoView);
        mIJKVideoView.setListener(listener);
        mIJKVideoView.setPath(videoUrl);
        mIJKVideoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIJKVideoView.start();
            }
        });


        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.bb);

        mYXJVideoVIew = findViewById(R.id.mYXJVideoVIew);

        controlVideoView = new YXJControlVideoView(this){
            @Override
            public void onFullScreen(boolean b) {
                super.onFullScreen(b);
                if (b){
                    ViewGroup.LayoutParams m = mYXJVideoVIew.getLayoutParams();
                    m.height = getResources().getDisplayMetrics().heightPixels ;
                    mYXJVideoVIew.setLayoutParams(m);
                }else {
                    ViewGroup.LayoutParams m = mYXJVideoVIew.getLayoutParams();
                    m.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    mYXJVideoVIew.setLayoutParams(m);
                }
            }
        };


        controlVideoView
                .isFullScreen(true)
                //是否支持底部控制栏 默认支持
                .isSupportBottomControl(true)
                //静音
                .isSupportVocClose(false)
                //支持自动播放
                .isSupportAutoPlay(true)
                //默认显示占位图 不然是一坨黑
                .setPlaceHolder(b)
                .isSupportChangeVolumeAndLight(true)
                //支持单击视频
                .setOnVideoClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                })
                //支持长按视频
                .setOnVideoLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return false;
                    }
                });
        mYXJVideoVIew.addControlView(controlVideoView);
        mYXJVideoVIew.setDataPath(videoUrl1, YXJVideoView.VideoType.URL);



        SoundPool.Builder builder = new SoundPool.Builder().setMaxStreams(3);
        AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
        //设置音频流的合适的属性
        attrBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
        //加载一个AudioAttributes
        builder.setAudioAttributes(attrBuilder.build());
        SoundPool soundPool = builder.build();
        int soundID = 0;
//        try {
//            AssetFileDescriptor fd = getAssets().openFd("ring_01");
//            soundID =  soundPool.load(fd,0);
//            soundPool.play(soundID,0.5f,0.5f,0,0,1.0f);
//            soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
//                @Override
//                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
//
//                }
//            });
//
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        final int voiceId = soundPool.load(this, R.raw.ring_01, 1);
        soundPool.play(voiceId,0.5f,0.5f,0,0,1.0f);
    }




    @Override
    protected void onResume() {
        super.onResume();
        controlVideoView.onResume();
    }


    @Override
    protected void onPause() {
        super.onPause();
        controlVideoView.onPause();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        controlVideoView.destroy();
    }


    private IJKVideoListener listener = new IJKVideoListener(){

        @Override
        public void onPrepared() {
            Log.v("[IJKVideoListener] - > ","onPrepared()");
        }

        @Override
        public void onCompletion() {
            Log.v("[IJKVideoListener] - > ","onCompletion()");
            mIJKVideoView.seekTo(0);
        }

        @Override
        public void onVideoSizeChanged(int i, int i1, int i2, int i3) {
            Log.v("[IJKVideoListener] - > ","onVideoSizeChanged()");
        }

        @Override
        public void onInfo(int i, int i1) {
            Log.v("[IJKVideoListener] - > ","onInfo() = "+ i +" , i1=  "+i1);
        }

        @Override
        public void onSeekComplete() {
            Log.v("[IJKVideoListener] - > ","onSeekComplete()");
        }

        @Override
        public void onBufferingUpdate(int i) {
            Log.v("[IJKVideoListener] - > ","onBufferingUpdate()  = "+i);
        }

        @Override
        public void onError(int i, int i1) {
            Log.v("[IJKVideoListener] - > ","onError()  = "+i+",i1 = "+i1);
        }
    };
}